package com.ziyueyuwen.xiniao.base;

import com.alibaba.fastjson.annotation.JSONField;
import com.suyeer.basic.bean.BaseHttpResContent;
import com.ziyueyuwen.xiniao.bean.ZyUser;
import com.ziyueyuwen.xiniao.bean.wx.TbWxUserInfo;
import com.ziyueyuwen.xiniao.enums.HttpStatusEnum;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;

import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndDataThenResponse;

/**
 * @author jun 2018/10/25
 */
public class HttpResContent<T> extends BaseHttpResContent<T> {
    private static final long serialVersionUID = 5640118292870299785L;
    private transient HttpStatusEnum httpStatus;

    public HttpResContent(HttpServletRequest request, HttpServletResponse response) {
        super(request, response);
        this.httpStatus = HttpStatusEnum.REPEAT_LOGIN;
    }

    public HttpResContent(String funcDes) {
        super();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        super.init(request, response);
        super.setFuncDes(funcDes);
        this.httpStatus = HttpStatusEnum.REPEAT_LOGIN;
    }

    public HttpStatusEnum getHttpStatus() {
        return httpStatus;
    }

    @Override
    public ZyUser getUserInfo() {
        return (ZyUser) super.userInfo;
    }

    @Override
    public TbWxUserInfo getWxUserInfo() {
        return (TbWxUserInfo) super.wxUserInfo;
    }

    @Override
    public Boolean isLogin() {
        if (!super.isLogin) {
            this.httpStatus = HttpStatusEnum.UNAUTHORIZED;
            return false;
        }
        return true;
    }

    @Override
    public Boolean isWxLogin() {
        return super.isWxLogin;
    }

    @JSONField(serialize = false)
    public Boolean isNotLogin() {
        return !isLogin();
    }

    @Override
    @JSONField(serialize = false)
    public Boolean isPermission() {
        HashSet<String> uriSet = new HashSet<>();
        uriSet.add("/course/get_all_course.do");
        uriSet.add("/question/course/get_course.do");
        uriSet.add("/question/get_question.do");
        uriSet.add("/student/get_all_student.do");
        uriSet.add("/user/get_sys_user.do");
        uriSet.add("/user/sync_ding_talk.do");
        uriSet.add("/student/save_student.do");
        if (!uriSet.contains(this.getReqURI())) {
            return true;
        }
        ZyUser zyUser = this.getUserInfo();
        if (zyUser == null) {
            return false;
        }
        return zyUser.getIfSysUser();
    }

    @JSONField(serialize = false)
    public Boolean isNotPermission() {
        Boolean flag = !isPermission();
        if (flag) {
            this.httpStatus = HttpStatusEnum.FORBIDDEN;
        }
        return flag;
    }

    public void printLogAndResponse(T data) {
        saveSuccessMsgAndDataThenResponse(this, data);
    }

}
